package com.jdaw.algorithm.binarysearch;

public class BinarySearchNoRe {
    public static void main(String[] args) {
        int arr[]={1,3,5,6};
        System.out.println(binarySearch(arr,2));
    }

    //二分查找的非递归实现
    public static int binarySearch(int[] arr,int target){
        int left=0;
        int right=arr.length-1;
        while(left<=right){
            int mid=(left+right)/2;
            if(arr[mid]<target){
                left=mid+1;
            }else if(arr[mid]>target){
                right=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
